package com.linkoog.devtools.code_generator.setting.configurable.ui;

import com.linkoog.devtools.code_generator.template.model.NodeData;
import com.linkoog.devtools.code_generator.template.model.node.FileNodeData;
import com.linkoog.devtools.code_generator.template.model.node.DirNodeData;
import com.linkoog.devtools.code_generator.template.model.node.TemplateNodeData;
import com.linkoog.devtools.code_generator.setting.configurable.ui.template.PanelBuilder;
import com.linkoog.devtools.code_generator.setting.configurable.ui.template.TemplateFilePanelBuilder;
import com.linkoog.devtools.code_generator.setting.configurable.ui.template.TemplateLeftPanelBuilder;
import com.linkoog.devtools.code_generator.setting.configurable.ui.template.TemplatePackagePanelBuilder;
import com.linkoog.devtools.code_generator.setting.configurable.ui.template.TemplateRootPanelBuilder;
import com.intellij.ui.GuiUtils;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class TemplateSettingPanel {

    private JPanel myMainPanel;

    private JPanel leftMainPanel;
    private JTree templateTree;

    private JPanel rightMainPanel;
    private CardLayout rightPanelCardLayout;

    TemplateRootPanelBuilder templateRootPanelBuilder;
    TemplatePackagePanelBuilder templatePackagePanelBuilder;
    TemplateFilePanelBuilder templateFilePanelBuilder;

    private static final String KEY_TEMPLATE_EMPTY = "empty";
    private static final String KEY_TEMPLATE_ROOT = "templateRoot";
    private static final String KEY_TEMPLATE_PACKAGE = "templatePackage";
    private static final String KEY_TEMPLATE_FILE = "templateFile";


    public TemplateSettingPanel() {
        $$$setupUI$$$();
        rightPanelCardLayout = (CardLayout) rightMainPanel.getLayout();
        templateRootPanelBuilder = new TemplateRootPanelBuilder();
        templatePackagePanelBuilder = new TemplatePackagePanelBuilder();
        templateFilePanelBuilder = new TemplateFilePanelBuilder();

        rightMainPanel.setPreferredSize(JBUI.size(500, 500));
        rightMainPanel.add(new JPanel(), KEY_TEMPLATE_EMPTY);
        rightMainPanel.add(templateRootPanelBuilder.getMyMainPanel(), KEY_TEMPLATE_ROOT);
        rightMainPanel.add(templatePackagePanelBuilder.getMyMainPanel(), KEY_TEMPLATE_PACKAGE);
        rightMainPanel.add(templateFilePanelBuilder.getMyMainPanel(), KEY_TEMPLATE_FILE);
        GuiUtils.replaceJSplitPaneWithIDEASplitter(myMainPanel);
    }


    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    public JPanel getLeftMainPanel() {
        return leftMainPanel;
    }

    public JTree getTemplateTree() {
        return templateTree;
    }

    public JPanel getRightMainPanel() {
        return rightMainPanel;
    }

    PanelBuilder currentPanelBuilder;


    public void showLeftContextPanel(NodeData nodeData) {
        if (currentPanelBuilder != null) {
            currentPanelBuilder.disappear();
        }

        if (nodeData instanceof TemplateNodeData) {
            TemplateNodeData templateNodeData = (TemplateNodeData) nodeData;
            templateRootPanelBuilder.resetUI(templateNodeData);
            rightPanelCardLayout.show(rightMainPanel, KEY_TEMPLATE_ROOT);
            currentPanelBuilder = templateRootPanelBuilder;
            return;
        } else if (nodeData instanceof DirNodeData) {
            DirNodeData dirNodeData = (DirNodeData) nodeData;
            templatePackagePanelBuilder.resetUI(dirNodeData);
            rightPanelCardLayout.show(rightMainPanel, KEY_TEMPLATE_PACKAGE);
            currentPanelBuilder = templatePackagePanelBuilder;
            return;
        } else if (nodeData instanceof FileNodeData) {
            FileNodeData fileNodeData = (FileNodeData) nodeData;
            templateFilePanelBuilder.resetUI(fileNodeData);
            rightPanelCardLayout.show(rightMainPanel, KEY_TEMPLATE_FILE);
            currentPanelBuilder = templateFilePanelBuilder;
            return;
        } else {
            rightPanelCardLayout.show(rightMainPanel, KEY_TEMPLATE_EMPTY);
        }
        currentPanelBuilder = null;
    }


    public List<TemplateNodeData> getTemplateDataList() {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) templateTree.getModel().getRoot();

        List<TemplateNodeData> templateNodeDataList = new ArrayList<>(root.getChildCount());
        Enumeration<TreeNode> enumeration = root.children();
        while (enumeration.hasMoreElements()) {
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) enumeration.nextElement();
            templateNodeDataList.add((TemplateNodeData) treeNode.getUserObject());
        }

        return templateNodeDataList;
    }


    private void createUIComponents() {
        // TODO: place custom component creation code here
        TemplateLeftPanelBuilder leftPanelBuilder = new TemplateLeftPanelBuilder(this);
        leftMainPanel = leftPanelBuilder.getMyMainPanel();
        templateTree = leftPanelBuilder.getTemplateTree();
    }


    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JSplitPane splitPane1 = new JSplitPane();
        splitPane1.setDividerLocation(270);
        splitPane1.setDividerSize(0);
        splitPane1.setEnabled(false);
        myMainPanel.add(splitPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(200, 200), null, 0, false));
        leftMainPanel.setMinimumSize(new Dimension(150, 24));
        splitPane1.setLeftComponent(leftMainPanel);
        rightMainPanel = new JPanel();
        rightMainPanel.setLayout(new CardLayout(0, 0));
        rightMainPanel.setMinimumSize(new Dimension(150, 24));
        splitPane1.setRightComponent(rightMainPanel);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        rightMainPanel.add(panel1, "Card1");
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }

}
